﻿using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace Hcf.Office.Word
{
    public class WordHelper
    {
        /// <summary>
        /// 合并文档,合并后的word文档，图片会丢失
        /// </summary>
        /// <param name="paths">word文档路径集合</param>
        /// <param name="breakNewPage">是否新页面</param>
        /// <returns></returns>
        public static byte[] MergeWordFiles1(string[] paths, bool breakNewPage = true)
        {
            using (var stream = new MemoryStream())
            {
                using (WordprocessingDocument mergedDocument = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document))
                {
                    MainDocumentPart mainPart = mergedDocument.AddMainDocumentPart();
                    mainPart.Document = new Document();
                    mainPart.Document.AppendChild(new Body());

                    for (int i = 0; i < paths.Length; i++)
                    {
                        var path = paths[i];
                        if (!File.Exists(path)) continue;

                        using (WordprocessingDocument document = WordprocessingDocument.Open(path, false))
                        {
                            foreach (var element in document.MainDocumentPart.Document.Body.Elements())
                            {
                                mergedDocument.MainDocumentPart.Document.Body.AppendChild(element.CloneNode(true));
                            }
                        }
                        //如果需要换新页面
                        if (breakNewPage && i < paths.Length - 1)
                        {
                            mergedDocument.MainDocumentPart.Document.Body.Append(new Paragraph(new Run(new Break() { Type = BreakValues.Page })));
                        }
                    }

                    mergedDocument.Save();
                }

                return stream.ToArray();
            }
        }

        /// <summary>
        /// 合并文档，合并后的文档图片正常
        /// </summary>
        /// <param name="paths">word文档路径集合</param>
        /// <param name="breakNewPage">是否新页面</param>
        /// <returns></returns>
        public static byte[] MergeWordFiles2(string[] paths, bool breakNewPage = true)
        {
            using (var stream = new MemoryStream())
            {
                using (WordprocessingDocument mergedDocument = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document))
                {
                    var mainPart = mergedDocument.AddMainDocumentPart();
                    mainPart.Document = new Document();
                    mainPart.Document.AppendChild(new Body());

                    for (var i = 0; i < paths.Length; i++)
                    {
                        var altChunkId = "cid_" + Guid.NewGuid().ToString().Replace("-", "");
                        var chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);

                        using (FileStream fileStream = File.Open(paths[i], FileMode.Open))
                        {
                            chunk.FeedData(fileStream);
                        }
                        var altChunk = new AltChunk();
                        altChunk.Id = altChunkId;

                        mainPart.Document.Append(altChunk);

                        //如果需要换新页面
                        if (breakNewPage && i < paths.Length - 1)
                        {
                            mainPart.Document.Append(new Paragraph(new Run(new Break() { Type = BreakValues.Page })));
                        }
                    }

                    mainPart.Document.Save();
                }

                return stream.ToArray();
            }
        }
    }
}
